package com.sghj.construction.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sghj.config.handler.ResultCode;
import com.sghj.config.handler.exceptionHandler.MyException;
import com.sghj.construction.entity.ConstructionSitePlan;
import com.sghj.construction.entity.PlanQuery;
import com.sghj.construction.entity.vo.ConstructionSitePlanVo;
import com.sghj.construction.mapper.ConstructionSitePlanMapper;
import com.sghj.construction.service.ConstructionSitePlanService;
import com.sghj.record.mapper.RecordOutcallMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 施工计划表 服务实现类
 * </p>
 *
 * @author fbb
 * @since 2024-07-11
 */
@Service
public class ConstructionSitePlanServiceImpl extends ServiceImpl<ConstructionSitePlanMapper, ConstructionSitePlan> implements ConstructionSitePlanService {

    @Resource
    private RecordOutcallMapper recordOutcallMapper;

    @Override
    public List<ConstructionSitePlan> selectByInfo(ConstructionSitePlan sitePlan) {
        return baseMapper.selectByInfo(sitePlan);
    }

    @Override
    public IPage<ConstructionSitePlanVo> selectByInfo(PlanQuery sitePlan, Page<ConstructionSitePlanVo> page) {
        return baseMapper.selectByInfo(page, sitePlan);
    }

    @Override
    public List<ConstructionSitePlan> selectByPhone(String monitoringPhone) {
        return baseMapper.selectByPhone(monitoringPhone);
    }

    @Override
    public ConstructionSitePlan selectByDayPlan(int dayPlanNumber) {
        return baseMapper.selectByDayPlan(dayPlanNumber);
    }

    @Override
    public ConstructionSitePlan selectByPlanAndPhone(int dayPlanNumber, String monitoringPhone) {
        return baseMapper.selectByPlanAndPhone(dayPlanNumber, monitoringPhone);
    }

    @Override
    public List<ConstructionSitePlan> selectCallInfo() {
        return baseMapper.selectCallInfo();
    }

    @Override
    public int insertConstruction(ConstructionSitePlan sitePlan) {

        //判断开始时间是否早与当前系统时间两小时后
        Date currentTime = new Date();
        // 获取当前时间的两小时后
        long twoHoursLater = currentTime.getTime() + 2 * 60 * 60 * 1000; // 2小时 = 2 * 60分钟 * 60秒 * 1000毫秒
        // 将 startTime 转换为时间戳
        long startTimeInMillis = sitePlan.getStartTime().getTime();

//        if (startTimeInMillis < twoHoursLater){
//            throw new MyException(ResultCode.FAIL,"计划开始时间晚于当前时间两小时后");
//        }

        //判断开始时间是否早于结束时间
        long endTimeInMillis = sitePlan.getEndTime().getTime();
        if (startTimeInMillis > endTimeInMillis){
            throw new MyException(ResultCode.FAIL,"计划开始时间晚于计划结束时间");
        }

        ConstructionSitePlan insertPlan = new ConstructionSitePlan();
        insertPlan.setDayPlanNumber(sitePlan.getDayPlanNumber());
        insertPlan.setLineType(sitePlan.getLineType());
        insertPlan.setLocationPlace(sitePlan.getLocationPlace());
        insertPlan.setConstructionSiteName(sitePlan.getConstructionSiteName());
        insertPlan.setStartTime(sitePlan.getStartTime());
        insertPlan.setEndTime(sitePlan.getEndTime());
        insertPlan.setMonitoringPersonnel(sitePlan.getMonitoringPersonnel());
        insertPlan.setMonitoringPhone(sitePlan.getMonitoringPhone());
        insertPlan.setMemo(sitePlan.getMemo());
        int num = baseMapper.insertConstruction(insertPlan);
        if (num > 0) {
            return 1;
        }
        return 0;
    }

    @Override
    public int updateConstructionById(ConstructionSitePlan sitePlan) {

        //通过id查询计划
        ConstructionSitePlan constructionSitePlan = baseMapper.selectById(sitePlan.getConstructionSiteId());

        //判断计划当前状态
        if (constructionSitePlan.getCallStatus() != 0){
            throw new MyException(ResultCode.FAIL,"当前计划已呼叫，不可修改");
        }

        //判断开始时间是否早与当前系统时间两小时后
        Date currentTime = new Date();
        // 获取当前时间的两小时后
        long twoHoursLater = currentTime.getTime() + 2 * 60 * 60 * 1000; // 2小时 = 2 * 60分钟 * 60秒 * 1000毫秒
        // 将 startTime 转换为时间戳
        long startTimeInMillis = sitePlan.getStartTime().getTime();

//        if (startTimeInMillis < twoHoursLater){
//            throw new MyException(ResultCode.FAIL,"计划开始时间晚于当前时间两小时后");
//        }

        //判断开始时间是否早于结束时间
        long endTimeInMillis = sitePlan.getEndTime().getTime();
        if (startTimeInMillis > endTimeInMillis){
            throw new MyException(ResultCode.FAIL,"计划开始时间晚于计划结束时间");
        }

        if (constructionSitePlan.getCallStatus() != 0){
            throw new MyException(ResultCode.FAIL,"当前计划已呼叫，不可删除");
        }

        int outCallNum = recordOutcallMapper.selectNumByDayPlanNumber(constructionSitePlan.getDayPlanNumber());
        if (outCallNum > 0){
            throw new MyException(ResultCode.FAIL,"当前计划已呼叫，不可删除");
        }

        ConstructionSitePlan updatePlan = baseMapper.selectById(sitePlan.getConstructionSiteId());
        updatePlan.setDayPlanNumber(sitePlan.getDayPlanNumber());
        updatePlan.setLineType(sitePlan.getLineType());
        updatePlan.setLocationPlace(sitePlan.getLocationPlace());
        updatePlan.setConstructionSiteName(sitePlan.getConstructionSiteName());
        updatePlan.setStartTime(sitePlan.getStartTime());
        updatePlan.setEndTime(sitePlan.getEndTime());
        updatePlan.setMonitoringPersonnel(sitePlan.getMonitoringPersonnel());
        updatePlan.setMonitoringPhone(sitePlan.getMonitoringPhone());
        updatePlan.setMemo(sitePlan.getMemo());
        updatePlan.setIsDelete(sitePlan.getIsDelete());
        int num = baseMapper.updateConstruction(updatePlan);
        if (num > 0) {
            return 1;
        }
        return 0;
    }

    @Override
    public int updateConstructionByDayPlan(ConstructionSitePlan sitePlan) {
        ConstructionSitePlan updatePlan = baseMapper.selectByDayPlan(sitePlan.getDayPlanNumber());
        updatePlan.setCallStatus(sitePlan.getCallStatus());
        updatePlan.setCallBackStatus(sitePlan.getCallBackStatus());
        int num = baseMapper.updateConstruction(updatePlan);
        if (num > 0) {
            return 1;
        }
        return 0;
    }

    @Override
    public int updatePlanCall(int dayPlanNumber, int callStatus) {
        int num = baseMapper.updatePlanCall(dayPlanNumber,callStatus);
        if (num > 0) {
            return 1;
        }
        return 0;
    }

    @Override
    public int updatePlanCallBack(int dayPlanNumber, int callBackStatus) {
        int num = baseMapper.updatePlanCallBack(dayPlanNumber,callBackStatus);
        if (num > 0) {
            return 1;
        }
        return 0;
    }

    @Override
    public List<ConstructionSitePlan> getAllUnIncallRecord() {
        List<ConstructionSitePlan> planList = baseMapper.getAllUnIncallRecord();
        return planList;
    }

    @Override
    public Integer selectNumByDayPlan(Integer dayPlanNumber, Integer constructionSiteId) {
        Integer haveNum = baseMapper.selectNumByDayPlan(dayPlanNumber,constructionSiteId);
        return haveNum;
    }

    @Override
    public int deleteConstructionById(Integer constructionSiteId) {
        ConstructionSitePlan constructionSitePlan = baseMapper.selectById(constructionSiteId);

        if (constructionSitePlan == null){
            throw new MyException(ResultCode.FAIL,"当前计划不存在");
        }

        if (constructionSitePlan.getIsDelete() == 1){
            throw new MyException(ResultCode.FAIL,"当前计划已删除");
        }

        if (constructionSitePlan.getCallStatus() != 0){
            throw new MyException(ResultCode.FAIL,"当前计划已呼叫，不可删除");
        }

        int outCallNum = recordOutcallMapper.selectNumByDayPlanNumber(constructionSitePlan.getDayPlanNumber());
        if (outCallNum > 0){
            throw new MyException(ResultCode.FAIL,"当前计划已呼叫，不可删除");
        }

        int deleteRes = baseMapper.deleteConstructionById(constructionSiteId);
        return deleteRes;
    }

    @Override
    public int updateNotCallBackRecord() {
        return baseMapper.updateNotCallBackRecord();
    }

    @Override
    public ConstructionSitePlan getPlanByDayPlanNumber(Integer planId) {
        return baseMapper.getPlanByDayPlanNumber(planId);
    }

    @Override
    public List<ConstructionSitePlanVo> searchPlanList(PlanQuery query) {
        if (query == null || query.getQueryStartTime() == null || query.getQueryEndTime() == null){
            throw new MyException(ResultCode.FAIL,"查询条件不足");
        }
        List<ConstructionSitePlanVo> planList = baseMapper.searchPlanList(query);
        return planList;
    }
}
